package com.cda.utils;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

import java.io.File;
import java.io.FileOutputStream;

/**
 * @ClassName: PDFBuild
 * @Auther: zhangyingqi
 * @Date: 2018/9/14 10:33
 * @Description:
 */
public class PDFBuild {

    public static void buidPDF(String pdfFile, String imageFile,
                               String waterMarkName, int permission) {
        try {
            File file = File.createTempFile("tempFile", ".pdf"); // 创建临时文件

            // 生成PDF
            if (createPDFFile(file)) {
                //waterMark(file.getPath(), imageFile, pdfFile, waterMarkName, permission); // 添加水印
                //waterMarkPicture(file.getPath(), imageFile, pdfFile); // 添加图片水印
                waterMarkPicture("C:\\Users\\zhulier\\Desktop\\123456_2018-09-07.pdf", imageFile, pdfFile); // 添加图片水印
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建PDF文件
     *
     * @param file
     *            临时文件
     * @return 成功/失败
     */
    public static boolean createPDFFile(File file) {
        Rectangle rect = new Rectangle(PageSize.A4);
        Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F);
        try {
            PdfWriter.getInstance(doc, new FileOutputStream(file));
            doc.open();

            BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
                    "Identity-H", true);// 使用系统字体

            Font font = new Font(bf, 14.0F, 0);
            font.setStyle(37); // 设置样式
            font.setFamily("宋体"); // 设置字体

            Paragraph p = new Paragraph("付 款 通 知 书\r\n", font);
            p.setAlignment(1);
            doc.add(p);
            doc.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void waterMarkPicture(String inputFile, String imageFile, String outputFile) {
        try {
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            int total = reader.getNumberOfPages() + 1;
            Image image = Image.getInstance(imageFile);
            float resizedWidht = image.getWidth();
            float resizedHeight = image.getHeight();
            float pageWidth = 0;
            float pageHeight = 0;
            //image.setRotationDegrees(30);//旋转 */
            //image.setRotation(30);//旋转 弧度
            PdfContentByte under;
            for (int i = 1; i < total; i++) {
                pageWidth = reader.getPageSize(i).getWidth();//获取pdf文档页面宽度
                pageHeight = reader.getPageSize(i).getHeight();//获取pdf文档页面高度
                // 图片位置
                //image.setAbsolutePosition(400, 480);
                image.setAbsolutePosition(pageWidth/2 - resizedWidht / 2, pageHeight/ 2 - resizedHeight / 2);
                under = stamper.getOverContent(i);
                // 添加水印文字
                under.setTextRise(-45);//斜度
                // 添加水印图片
                under.addImage(image);
                PdfGState gs = new PdfGState();
                gs.setFillOpacity(0.2f);// 设置透明度为0.2
                under.setGState(gs);
            }
            stamper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void waterMark(String inputFile, String imageFile,
                                 String outputFile, String waterMarkName, int permission) {
        try {
            PdfReader reader = new PdfReader(inputFile);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                    outputFile));

            BaseFont base = BaseFont.createFont(
                    "C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体

            int total = reader.getNumberOfPages() + 1;
            Image image = Image.getInstance(imageFile);

            // 图片位置
            image.setAbsolutePosition(400, 480);
            PdfContentByte under;
            int j = waterMarkName.length();
            char c = 0;
            int rise = 0;
            for (int i = 1; i < total; i++) {
                rise = 400;
                under = stamper.getUnderContent(i);
                under.beginText();
                under.setFontAndSize(base, 30);

                if (j >= 15) {
                    under.setTextMatrix(200, 120);
                    for (int k = 0; k < j; k++) {
                        under.setTextRise(rise);
                        c = waterMarkName.charAt(k);
                        under.showText(c + "");
                    }
                } else {
                    under.setTextMatrix(240, 100);
                    for (int k = 0; k < j; k++) {
                        under.setTextRise(rise);
                        c = waterMarkName.charAt(k);
                        under.showText(c + "");
                        rise -= 18;

                    }
                }
                PdfGState gs = new PdfGState();
                gs.setFillOpacity(0.2f);// 设置透明度为0.2
                under.setGState(gs);
                // 添加水印文字
                under.endText();
                under.setTextRise(-45);//斜度
                // 添加水印图片
                under.addImage(image);
                // 画个圈
                under.ellipse(250, 450, 350, 550);
                under.setLineWidth(1f);
                under.stroke();
            }
            stamper.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String imageFilePath = "C://mysoftware//images//upload//report//water7.png"; // 水印图片路径
        String pdfFilePath = "D:/itext.pdf"; // 文件生成路径
        buidPDF(pdfFilePath, imageFilePath, "正版授权", 16);
    }

}
